﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Xml;
using log4net;
using log4net.Config;

namespace Utility
{
    /// <summary>
    /// 系统日志类
    /// </summary>
    public class Log
    {
        public enum TypeOfSysLog { Error, Warning, Ordinary };
        public static readonly log4net.ILog loginfo = log4net.LogManager.GetLogger("loginfo");
        public static readonly log4net.ILog logerror = log4net.LogManager.GetLogger("logerror");

        public static void Init()
        {
            string path = string.Format("{0}log4net.config", AppDomain.CurrentDomain.BaseDirectory);
            if (File.Exists(path))
            {
                log4net.Config.XmlConfigurator.Configure(new FileInfo(path));
            }
        }

        public static void Info(string info)
        {
            if (loginfo.IsInfoEnabled)
            {
                loginfo.Info(info);
            }
        }

        /// <summary>
        /// 错误记录
        /// </summary>
        /// <param name="info">附加信息</param>
        /// <param name="ex">错误</param>
        public static void Error(string info, Exception ex)
        {
            if (!string.IsNullOrEmpty(info) && ex == null)
            {
                logerror.ErrorFormat("【附加信息】 : {0}<br>", new object[] { info });
            }
            else if (!string.IsNullOrEmpty(info) && ex != null)
            {
                string errorMsg = ErrorMsg(ex);
                logerror.ErrorFormat("【附加信息】 : {0}<br>{1}", new object[] { info, errorMsg });
            }
            else if (string.IsNullOrEmpty(info) && ex != null)
            {
                string errorMsg = ErrorMsg(ex);
                logerror.Error(errorMsg);
            }
        }

        /// <summary>
        ///错误信息
        /// </summary>
        /// <param name="ex">异常</param>
        /// <returns>错误信息</returns>
        private static string ErrorMsg(Exception ex)
        {
            string errorMsg = string.Format("【异常类型】：{0} \r\n【异常信息】：{1} \r\n【堆栈调用】：{2}\r\n", new object[] { ex.GetType().Name, ex.Message, ex.StackTrace });
            return errorMsg;
        }

        /// <summary>
        /// 记录日志至文本文件
        /// </summary>
        /// <param name="txtlogs)"></param>
        public static int SystemLog(string txtlogs)
        {
            DateTime dt = DateTime.Now;
            string s_dt = dt.ToString("yyyyMMddHH");
            string s_year = s_dt.Substring(0, 4);
            string s_month = s_dt.Substring(4, 2);
            string s_day = s_dt.Substring(6, 2);
            string s_logsPath = SystemTools.GetCurrentDirectory() + "\\Logs\\" + s_year;

            if (!Directory.Exists(s_logsPath))
            {
                Directory.CreateDirectory(s_logsPath);
            }
            s_logsPath += "\\" + s_month;
            if (!Directory.Exists(s_logsPath))
            {
                Directory.CreateDirectory(s_logsPath);
            }
            s_logsPath += "\\" + s_day;
            if (!Directory.Exists(s_logsPath))
            {
                Directory.CreateDirectory(s_logsPath);
            }
            string txtlogsFileName = s_logsPath + "\\" + s_dt + ".log";
            try
            {
                using (StreamWriter writer = new StreamWriter(txtlogsFileName, true, Encoding.Default))
                {
                    writer.WriteLine(DateTime.Now.ToString() + " " + txtlogs);
                }
            }
            catch
            {
                return 0;
            }
            return 1;
        }
    }
}
